import { Image as VanImage } from "vant";
import yzService from "components/ui_components/yz_service";
import { scrollMixin } from "utils/mixin";
export default {
  components: {
    VanImage,
    yzService
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      parentId: "", //二级ID
      query: {
        page: 1,
        longitude: "",
        latitude: "",
        city: "",
        cityCode: "",
        travel_around_goods_type: "",
        is_recommand: "",
        category: "",
        title: "",
        basic_info:1
      },
      active: 0,
      timer: null, //定时器
      secondaryIndex: 0,
      serviceShow: false,
      stairClassify: [],
      secondary: [],
      categoriesGoodsList: [],
      serviceQRcode: "",
      serviceMobile: "",
      cservice: "",
      total_page: 0,
      isLoadMore: true,
      latitude:'',
      longitude:'',
      goodsFlag:false,
    };
  },
  created() {
    this.active = Number(this.$route.query.childrenId);
    this.query.city = this.$route.query.city;
    this.query.cityCode = this.$route.query.cityCode;
    this.query.latitude = this.$route.query.latitude;
    this.query.longitude = this.$route.query.longitude;
    
  },
  activated() {
    document.documentElement.scrollTop = 0;
    this.active = Number(this.$route.query.childrenId);
    this.query.page = 1;
    this.getCategory();
    this.getGood();
  },
  methods: {
    secondaryOn(index) {
      this.secondaryIndex = index;
      this.query.category = Number(this.secondary[index].id);
      this.query.page = 1;
      this.getGoodsList();
    },
    searchOn() {
      this.$router.push(this.fun.getUrl("tourAroundTourSearch", {}, { keyWord: this.query.title,city: this.query.city,cityCode: this.query.cityCode,latitude: this.query.latitude ,longitude:this.query.longitude }));
    },
    firstOn(id) {
      this.parentId = id;
      this.query.page = 1;
      // this.getCategoryByParentId();
      // //修复点击进去商品详情后，再返回时，tab总会搜索第一个
      const query = JSON.parse(JSON.stringify(this.$route.query))
      query.childrenId = this.active;//title 修改的参数
      this.$router.push({ path: this.$route.path, query });
    },
    showService() {
      this.serviceShow = true;
    },
    getGood() {
      $http
        .get("goods.customer-service.index", { goods_id: "" })
        .then(response => {
          if (response.result === 1) {
            let data = response.data;
            this.serviceQRcode = data.service_QRcode;
            this.serviceMobile = data.service_mobile;
            this.cservice = data.cservice;
          } else {
            this.$toast(response.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getCategory() {
      //一级分类
      $http.get("plugin.travel-around.frontend.goods.goods.getCategory", {}, "加载中..").then(response => {
        if (response.result == 1) {
          this.stairClassify = response.data;
          this.parentId = this.active;
          // this.getCategoryByParentId();
          this.query.category = Number(this.parentId);
          this.getGoodsList();
        } else {
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    getCategoryByParentId() {
      //二级分类
      let data = {
        parent_id: this.parentId
      };
      $http.get("plugin.travel-around.frontend.goods.goods.getCategoryByParentId", data, "加载中..").then(response => {
        if (response.result == 1) {
          this.secondary = response.data;
          this.query.category = Number(this.secondary[this.secondaryIndex].id);
          this.getGoodsList();
        } else {
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    goodsNavTo(id) {
      this.$router.push(this.fun.getUrl("tourAroundGoodsDetails", {}, { goodsId: id ,longitude:this.query.longitude,latitude:this.query.latitude}));
    },
    getGoodsList() {
      this.goodsFlag = false;
      //商品类别
      $http.post("plugin.travel-around.frontend.goods.goods.getGoodsList", this.query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let basicInfo = response.basic_info;
          this.isLoadMore = true;
          this.total_page = res.last_page;
          if (!this.total_page) {
            this.query.page = 0;
          }
          this.goodsFlag = true;
          this.categoriesGoodsList = res.data;
          this.$nextTick(() => {
            this.fun.wxShare(
              "",
              { mid: this.fun.getKeyByMid() },
              {
                link: window.location.href,
                title: '周边游-'+basicInfo?.home.mailInfo?.name,
                imgUrl: basicInfo?.home.mailInfo?.logo_url,
                description: '全国周边吃喝玩乐特惠套餐！'
              }
            );
          },50);
        } else {
          this.goodsFlag = true;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.query.page >= this.total_page) {
        return;
      } else {
        this.query.page = this.query.page + 1;
        $http.post("plugin.travel-around.frontend.goods.goods.getGoodsList", this.query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.categoriesGoodsList = [...this.categoriesGoodsList, ...myData]; //数组拼接
            } else {
              this.query.page = this.query.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    }
  }
};
